<template>
  <div class="app-container search-table-box aidex-table">
    <el-card shadow="never" ref="queryRef" style="margin-bottom: 12px;" class="search_card" v-show="showSearch">
      <div class="filter-container">
        <div class="search_box">
          <el-form :model="queryParams" ref="queryForm" label-width="80px" size="mini">
            <el-row :gutter="16">
              <el-col :md="6" >
                <el-form-item label="购方ID" prop="buyerId">
                  <el-input
                    v-model="queryParams.buyerId"
                    placeholder="请输入购方ID"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" >
                <el-form-item label="客户ID" prop="custId">
                  <el-input
                    v-model="queryParams.custId"
                    placeholder="请输入客户ID"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" >
                <el-form-item label="公司代码" prop="siteCode">
                  <el-input
                    v-model="queryParams.siteCode"
                    placeholder="请输入公司代码"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="公司" prop="siteName">
                  <el-input
                    v-model="queryParams.siteName"
                    placeholder="请输入公司"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="邮箱" prop="buyerMail">
                  <el-input
                    v-model="queryParams.buyerMail"
                    placeholder="请输入邮箱"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="是否默认代码" prop="isDefault">
                  <el-input
                    v-model="queryParams.isDefault"
                    placeholder="请输入是否默认代码"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="是否默认" prop="isDefaultName">
                  <el-input
                    v-model="queryParams.isDefaultName"
                    placeholder="请输入是否默认"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="购方名称" prop="mobile">
                  <el-input
                    v-model="queryParams.mobile"
                    placeholder="请输入购方名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="是否电票交付信息代码" prop="isElecTicket">
                  <el-input
                    v-model="queryParams.isElecTicket"
                    placeholder="请输入是否电票交付信息代码"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="是否电票交付信息" prop="isElecTicketName">
                  <el-input
                    v-model="queryParams.isElecTicketName"
                    placeholder="请输入是否电票交付信息"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="收件人" prop="receiveName">
                  <el-input
                    v-model="queryParams.receiveName"
                    placeholder="请输入收件人"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="收件地址" prop="receiveAddress">
                  <el-input
                    v-model="queryParams.receiveAddress"
                    placeholder="请输入收件地址"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="联系电话" prop="receiveTel">
                  <el-input
                    v-model="queryParams.receiveTel"
                    placeholder="请输入联系电话"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" align="right" style="float: right;">
                <el-form-item>
                  <el-button class="filter-item" type="primary" @click="handleQuery">搜索</el-button>
                  <el-button class="filter-item" style="margin-left: 8px" @click="resetQuery">重置</el-button>
                  <a @click="toggleAdvanced" style="margin:0 4px 0 8px ;vertical-align: middle;">
                    {{ advanced ? '收起' : '展开' }}
                    <i :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
                  </a>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-card>
    <el-card shadow="never" >
      <template #header>
        <el-row>
          <el-col :span="8">
            <div class="card-header">
              <el-button disabled type="text">购方联系人信息</el-button>
            </div>
          </el-col>
          <el-col :span="16">
            <div class="btn_box" align="right" style="float: right;">
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                type="primary"
                icon="el-icon-plus"
                @click="handleAdd"
                size="mini"
                v-hasPermi="['saleconfig:contact:add']"
              >新增</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-delete"
                type="danger"
                :disabled="multiple"
                @click="handleDelete"
                size="mini"
                v-if="!multiple"
                v-hasPermi="['saleconfig:contact:remove']"
              >删除</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-download"
                @click="handleExport"
                size="mini"
                v-hasPermi="['saleconfig:contact:export']"
              >导出</el-button>
              <right-toolbar :showSearch.sync="showSearch" @showSearchFun="showSearchFun" @queryTable="getList"></right-toolbar>
            </div>
          </el-col>
        </el-row>
      </template>

      <el-table
              border
              stripe
              default-expand-all
              ref="tableRef"
              v-loading="loading"
              :data="contactList"
              @selection-change="handleSelectionChange"
              :row-style="rowClass"
              :header-cell-style= "{
                'font-size': '14px !important',
                'font-weight': '500 !important',
                'background-color':'rgb(241 241 241 / 52%)'
              }"
              highlight-current-row style="width: 100%;"
              :height="tableHeight"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="联系人ID" align="center" prop="entryId" />
        <el-table-column label="购方ID" align="center" prop="buyerId" />
        <el-table-column label="客户ID" align="center" prop="custId" />
        <el-table-column label="公司代码" align="center" prop="siteCode" />
        <el-table-column label="公司" align="center" prop="siteName" />
        <el-table-column label="邮箱" align="center" prop="buyerMail" />
        <el-table-column label="是否默认代码" align="center" prop="isDefault" />
        <el-table-column label="是否默认" align="center" prop="isDefaultName" />
        <el-table-column label="购方名称" align="center" prop="mobile" />
        <el-table-column label="是否电票交付信息代码" align="center" prop="isElecTicket" />
        <el-table-column label="是否电票交付信息" align="center" prop="isElecTicketName" />
        <el-table-column label="收件人" align="center" prop="receiveName" />
        <el-table-column label="收件地址" align="center" prop="receiveAddress" />
        <el-table-column label="联系电话" align="center" prop="receiveTel" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['saleconfig:contact:edit']"
            >修改</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button
              type="text"
              style="color: red;"
              @click="handleDelete(scope.row)"
              v-hasPermi="['saleconfig:contact:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <svg-icon icon-class="search-none" style="font-size: 64px;" />
          <p>暂无数据</p>
        </div>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改购方联系人对话框 -->
      <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="720px" top="10vh" append-to-body>
        <div style="height: 490px;overflow: auto; padding: 12px 24px;">
          <el-form ref="form" size="mini" :model="form" :rules="rules" label-position="top">
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="购方ID" prop="buyerId">
                  <el-input v-model="form.buyerId" placeholder="请输入购方ID" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="客户ID" prop="custId">
                  <el-input v-model="form.custId" placeholder="请输入客户ID" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="公司代码" prop="siteCode">
                  <el-input v-model="form.siteCode" placeholder="请输入公司代码" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="公司" prop="siteName">
                  <el-input v-model="form.siteName" placeholder="请输入公司" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="邮箱" prop="buyerMail">
                  <el-input v-model="form.buyerMail" placeholder="请输入邮箱" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否默认代码" prop="isDefault">
                  <el-input v-model="form.isDefault" placeholder="请输入是否默认代码" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否默认" prop="isDefaultName">
                  <el-input v-model="form.isDefaultName" placeholder="请输入是否默认" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="购方名称" prop="mobile">
                  <el-input v-model="form.mobile" placeholder="请输入购方名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否电票交付信息代码" prop="isElecTicket">
                  <el-input v-model="form.isElecTicket" placeholder="请输入是否电票交付信息代码" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否电票交付信息" prop="isElecTicketName">
                  <el-input v-model="form.isElecTicketName" placeholder="请输入是否电票交付信息" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="收件人" prop="receiveName">
                  <el-input v-model="form.receiveName" placeholder="请输入收件人" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="收件地址" prop="receiveAddress">
                  <el-input v-model="form.receiveAddress" placeholder="请输入收件地址" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="receiveTel">
                  <el-input v-model="form.receiveTel" placeholder="请输入联系电话" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary"  size="mini" @click="submitForm">确 定</el-button>
          <el-button  size="mini" @click="cancel">取 消</el-button>
        </div>
      </el-dialog>



    </el-card>
  </div>
</template>

<script>
import { listContact, getContact, delContact, addContact, updateContact } from "@/api/saleconfig/contact";
/* 城市列表 **/
import { provinceList, cityList, districtList } from "@/api/system/city";

export default {
  name: "Contact",
  data() {
    return {
      advanced: false,
      tableHeight: this.getInitTableHeight(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购方联系人表格数据
      contactList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{

        },
        buyerId: null,
        custId: null,
        siteCode: null,
        siteName: null,
        buyerMail: null,
        isDefault: null,
        isDefaultName: null,
        mobile: null,
        isElecTicket: null,
        isElecTicketName: null,
        receiveName: null,
        receiveAddress: null,
        receiveTel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      /* ======================================省市区======================================  */
      // 省
      provinceId: null,
      provinceList:[],
      // 市
      cityId: null,
      cityList:[],
      // 区
      districtId: null,
      districtList:[],

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 行样式 */
    rowClass({ row, rowIndex }) {
      if (this.ids.includes(row.id)) {
          return { background: "#d9edf7", height:'20px' };
      } else {
          return { height:'20px'};
      }
    },
   /** 隐藏搜索按钮操作 */
   showSearchFun(isShowSearch){
      this.showSearch = isShowSearch
      let oldHeight = this.$refs.queryRef.$el.offsetHeight
      if(!isShowSearch){
          //当前是显示状态
          oldHeight = oldHeight + 12
      }else{
          oldHeight = oldHeight - 12
      }
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 展开按钮操作 */
    toggleAdvanced() {
      const oldHeight = this.$refs.queryRef.$el.offsetHeight
      this.advanced = !this.advanced
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 查询购方联系人列表 */
    getList() {
      this.loading = true;
      listContact(this.queryParams).then(response => {
        this.contactList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        entryId: null,
        buyerId: null,
        custId: null,
        siteCode: null,
        siteName: null,
        buyerMail: null,
        isDefault: null,
        isDefaultName: null,
        mobile: null,
        isElecTicket: null,
        isElecTicketName: null,
        receiveName: null,
        receiveAddress: null,
        receiveTel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },


      /* ※※ 查询 省市区 相关方法 start ※※ */
      // 获取省
      getProvinceList(){
          provinceList().then(response => {
              this.provinceList = response.data;
          });
      },
      // 获取市
      getCityList(){
          console.log(this.provinceId);
          cityList( {"parentId" : this.provinceId } ).then(response => {
              this.cityList = response.data;
          });
      },
      // 获取区县
      getDistrictList(){
          districtList( {"parentId" : this.cityId } ).then(response => {
              this.districtList = response.data;
          });
      },
      // 选中省份
      handleChangeProvince(val){
          // 给表单省字段赋值
          this.form.provinceid = val;
          let province = {};
          province = this.provinceList.find((item)=>{
              return item.id === val;
          });
          this.form.province = province.name;
          // 城市数据清空、区县数据清空;
          this.cityList = [];
          this.districtList = [];
          // 已选市、区数据清空
          this.cityId = null;
          this.districtId = null;
          // 表单城市、区县数据清空
          this.form.cityid = null;
          this.form.city = null;
          this.form.areaid = null;
          this.form.area = null;
          // 获取城市数据
          this.getCityList();
      },
      // 清空省份
      handleClearProvince(){
          // 城市数据清空、区县数据清空;
          this.cityList = [];
          this.districtList = [];
          // 已选市、区数据清空
          this.cityId = null;
          this.districtId = null;
          // 表单城市、区县数据清空
          this.form.cityid = null;
          this.form.city = null;
          this.form.areaid = null;
          this.form.area = null;
      },
      // 选中城市
      handleChangeCity(val){
          // 给表单市字段赋值
          this.form.cityid = val;
          let city = {};
          city = this.cityList.find((item)=>{
              return item.id === val;
          });
          this.form.city = city.name;
          // 区县数据清空
          this.districtList = [];
          // 已选区县清空
          this.districtId = null;
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
          // 获取城市数据
          this.getDistrictList();
      },
      // 清空省份
      handleClearCity(){
          // 区县数据清空
          this.districtList = [];
          // 已选区县清空
          this.districtId = null;
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
      },
      // 选中区县
      handleChangeDistrict(val){
          // 给表单省字段赋值
          this.form.areaid = val;
          let district = {};
          district = this.districtList.find((item)=>{
              return item.id === val;
          });
          this.form.area = district.name;
      },
      // 清空省份
      handleClearDistrict(){
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
      },

      /* ※※ 查询 省市区 相关方法 end ※※ */




    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.entryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购方联系人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const entryId = row.entryId || this.ids
      getContact(entryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改购方联系人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.entryId != null) {
            updateContact(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContact(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const entryIds = row.entryId || this.ids;
      this.$modal.confirm('是否确认删除购方联系人编号为"' + entryIds + '"的数据项？').then(function() {
        return delContact(entryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('saleconfig/contact/export', {
        ...this.queryParams
      }, `contact_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
